package com.szqhyxj.youxiangju.sys.components;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.szqhyxj.youxiangju.sys.imag.RotateTransformation;

import java.io.ByteArrayOutputStream;
import java.io.File;

/**
 * Created by qin
 * Date 2018/10/19
 * About
 */
public class LoadImageUtils{
    /**
     * 加载本地图片
     *
     * @param imageView
     * @param file
     */
    public static void loadImage(ImageView imageView, File file) {
        Glide.with(imageView.getContext()).load(file).into(imageView);
    }


    /**
     * 当宽小于高4/5时顺时针旋转
     * 加载本地图片旋转
     *
     * @param imageView
     * @param file
     */
    public static void loadImageRotation(ImageView imageView, File file,float rotateRotationAngle) {
        RequestOptions myOptions = new RequestOptions().transform(new RotateTransformation(rotateRotationAngle));
        Glide.with(imageView.getContext())
                .load(file)
                .apply(myOptions)
                .into(imageView);
    }

    /**
     * 当宽小于高4/5时顺时针旋转
     * 加载本地图片旋转
     * @param imageView
     * @param file
     */
    public static void loadImageRotation(ImageView imageView, String file,float rotateRotationAngle) {
        RequestOptions myOptions = new RequestOptions().transform(new RotateTransformation(rotateRotationAngle));
        Glide.with(imageView.getContext())
                .load(file)
                .apply(myOptions)
                .into(imageView);
    }

    /**
     * 加载本地图片
     *
     * @param imageView
     * @param file
     * @param radius
     */
    public static void loadImage(ImageView imageView, File file, int radius) {
        RoundedCorners roundedCorners = new RoundedCorners(radius);
        RequestOptions requestOptions = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(imageView.getContext()).load(file).apply(requestOptions).into(imageView);
    }

    /**
     * 加载bimap
     * @param imageView
     * @param bitmap
     * @param radius
     */
    public static void loadImage(ImageView imageView, Bitmap bitmap, int radius) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        RoundedCorners roundedCorners = new RoundedCorners(radius);
        RequestOptions requestOptions = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(imageView.getContext()).load(bytes).apply(requestOptions).into(imageView);
    }


    /**
     * 加载网络图片
     *
     * @param imageView
     * @param url
     * @param radius
     */
    public static void loadImage(ImageView imageView, String url, int radius) {
        RoundedCorners roundedCorners = new RoundedCorners(radius);
        RequestOptions requestOptions = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }

    /**
     * 加载网络图片
     *
     * @param imageView
     * @param url
     */
    public static void loadImage(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .load(url)
                .into(imageView);
    }
    /**
     * 加载网络图片
     *
     * @param imageView
     * @param url
     * @param drawableHolder 加载中图片
     * @param errorDrawable  加载错误图片
     * @param radius         圆角
     */
    public static void loadImage(ImageView imageView, String url, Drawable drawableHolder, Drawable errorDrawable, int radius) {
        RoundedCorners roundedCorners = new RoundedCorners(radius);
        RequestOptions requestOptions = RequestOptions.bitmapTransform(roundedCorners);
        requestOptions.placeholder(drawableHolder);
        requestOptions.error(errorDrawable);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }

}
